package com.wqj.kyee.gps.bluetooth

import android.content.Intent
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.Poi
import com.amap.api.services.core.PoiItem
import com.wqj.kyee.gps.R
import com.wqj.kyee.gps.nav.MapActivity
import com.wqj.kyee.gps.nav.RouteShowActivity
import com.wqj.kyee.gps.util.Constants

/**
 * Created by kyee on 17-12-8.
 */
class PotListAdapter(var mList: ArrayList<PoiItem>, var context: MapActivity) : BaseAdapter() {

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var holder: MyViewHolder
        var info: PoiItem = mList[position]
        //重用view
        var v: View
        if (convertView == null) {
            holder = MyViewHolder()
            v = LayoutInflater.from(context).inflate(R.layout.pot_list_item, parent, false)
            holder.info1 = v.findViewById<TextView>(R.id.info1) as TextView
            holder.info2 = v.findViewById<TextView>(R.id.info2) as TextView
            holder.routeSearch = v.findViewById<TextView>(R.id.routeSearch) as TextView
            //设置tag
            v.tag = holder
        } else {
            v = convertView
            //获取tag并强转
            holder = v.tag as MyViewHolder
        }

        //为TextView设置内容
        holder.info1.text = info.title
        holder.info2.text = info.snippet
        //holder.textView.background = ContextCompat.getDrawable(context, R.mipmap.item)
        holder.routeSearch.setOnClickListener{ v ->
            Log.d("wqj", "will start nav：" + info.title + ", " + position)

            // 发送目标地址到导航页面
            //var intent = Intent(context, RouteSelectActivity::class.java)
            //intent.putExtra("lat", info.latLonPoint.latitude)
            //intent.putExtra("lng", info.latLonPoint.longitude)
            //intent.putExtra("title", holder.info1.text.toString())
            var intent = Intent(context, RouteShowActivity::class.java)
            intent.putExtra(Constants.EXTRA_CURRENT_LAT, MapActivity.curLat)
            intent.putExtra(Constants.EXTRA_CURRENT_LNG, MapActivity.curLng)
            intent.putExtra(Constants.EXTRA_CURRENT_CITY, MapActivity.curCity)
            intent.putExtra(Constants.EXTRA_POI, Poi(info.title, LatLng(info.latLonPoint.latitude, info.latLonPoint.longitude), info.poiId))
            context.startActivity(intent)
        }
        return v
    }

    override fun getItem(position: Int): Any {
        return mList[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getCount(): Int {
        return mList.size
    }

    class MyViewHolder {

        lateinit var info1: TextView
        lateinit var info2: TextView
        lateinit var routeSearch: TextView

    }
}